package com.lbssoft.gpsbd.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lbssoft.gpsbd.common.HttpUtil;
import com.lbssoft.gpsbd.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api("扩展接口")
@RestController
@RequestMapping(value="/api")
public class ApiController {

	
	/**
	 * 经纬度解析为中文地址
	 * type=1 高德
	 * type=2 百度
	 * @param location
	 * @return
	 */
	@RequestMapping(value = "/geocoder",method = {RequestMethod.GET})
	public Result geocoder(@RequestParam("location") String location){

		Result result = new Result();
		HashMap params = new HashMap();
		Result res =  new Result();
		Map<String,String> address = new HashMap();
//		if(type==1){
//			params.put("key", "a32de0b7de54173e304c517962246be5");
//			params.put("location", location);
//			res = HttpUtil.get("http://restapi.amap.com/v3/geocode/regeo", params);
//			
//		}else 
		//if(type==2){
			params.put("ak", "4BhgRmXzX6R0hDGhBpoEmppZx8pCIdgh");
			params.put("coordtype", "wgs84ll");//gcj02ll高德 wgs84llgps bd09ll百度
			params.put("location", location);
			params.put("output", "json");
			res = HttpUtil.get("http://api.map.baidu.com/geocoder/v2/", params);
			if(res.getCode()==0||res.getCode()==200){
				JSONObject obj = JSON.parseObject(JSON.parseObject(res.getData().toString()).getString("result"));
				address.put("address", obj.getString("formatted_address"));
				address.put("description", obj.getString("sematic_description"));
			}
		//}
		result.setData(address);
		
		
		
		return result;
	}

	@ApiOperation(value="测试接口", notes="测试接口")
	@RequestMapping(value = "/subscribe",method = {RequestMethod.POST})
	@ResponseBody
	public Result subscribe(@Validated @RequestBody Map map) {
		Result result = new Result();
		result.setData(map);
		result.setMsg("接受数据成功");
		return result;
	}
	
}